package com.bkhech.bigdata.akka

import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ActorRef, ActorSystem, Behavior}
import lombok.extern.slf4j.Slf4j

/**
 * Actor 通信模型
 *    1. Actor 相当于 Java 中的 Thread
 *       2. Spark1.6 节点之间通信使用的是Akka, Akka 底层实现就是 Actor
 *       3. Spark2.3.1 底层实现改变成了 Netty
 *
 * @author guowm
 * @date 2022/7/11
 */
@Slf4j
object AkkaActorDemo {
    final case class Greet(whom: String, replyTo: ActorRef[Greeted])
    final case class Greeted(whom: String, from: ActorRef[Greet])

    def apply(): Behavior[Greet] = Behaviors.receive { (context, message) =>
      context.log.info("Hello {}!", message.whom)
      message.replyTo ! Greeted(message.whom, context.self)
      Behaviors.same
    }

    def main(args: Array[String]): Unit = {
      val system = ActorSystem(AkkaActorDemo(), "hello")

//      system ! AkkaActorDemo.apply("World")
//      system ! AkkaActorDemo.apply("Akka")
    }
}
